package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 订单分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String number, String beginTime,String endTime){
        log.info("订单分页查询:page:{},pageSize:{},number:{},beginTime:{},endTime:{}",page,pageSize,number,beginTime,endTime);
        //设置分页条件
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //设置查询条件
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(number!=null,Orders::getNumber,number);
        //把时间从String->LocalDateTime
        if(beginTime!=null&&endTime!=null){
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime beginDateTime = LocalDateTime.parse(beginTime,dateTimeFormatter);
            LocalDateTime endDateTime = LocalDateTime.parse(endTime,dateTimeFormatter);
//            System.out.println("beginDateTime -> "+beginDateTime+"   and   endDateTime -> "+endDateTime);
            wrapper.between(Orders::getOrderTime,beginDateTime,endDateTime);
        }
        //查询
        ordersService.page(ordersPage,wrapper);
        return R.success(ordersPage);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody Orders orders){
        UpdateChainWrapper<Orders> wrapper = ordersService.update();
        wrapper.eq(orders.getId()!=null,"id",orders.getId())
                .set(orders.getStatus()!=null,"status",orders.getStatus())
                .update();
        return R.success("成功");
    }

    /**
     * 用户订单的分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R userPage(Integer page,Integer pageSize){
        log.info("用户订单的分页查询page:{} pageSize:{}",page,pageSize);
        Page<OrdersDto> ordersDtoPage = ordersService.pageWithDetail(page,pageSize);
        return R.success(ordersDtoPage);
    }

    /**
     * 用户下单
     * @param orders
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders, HttpSession session){
        Long userId = (Long) session.getAttribute("userId");
        log.info("顾客'{}'发起订单:{}",userId,orders);
        boolean b = ordersService.submit(orders,userId);
        return R.success("成功");





    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R again(@RequestBody Orders orders,HttpSession session){
        log.info("用户再来一单");
        Long userId = (Long) session.getAttribute("userId");
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(orders.getId()!=null,OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> list = orderDetailService.list(wrapper);
        for (OrderDetail orderDetail : list) {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(userId);
            BeanUtils.copyProperties(orderDetail,shoppingCart);
            shoppingCartService.save(shoppingCart);
        }
        return R.success(list);
    }
}
